package cn.qicaiwang.open.qxyApi.util;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;

public class IDCardUtils {

    public static int calculateAgeFromIDCard(String idCardNumber) {
        // 检查身份证号长度是否为18位
        if (idCardNumber == null || idCardNumber.length() != 18) {
            throw new IllegalArgumentException("Invalid ID card number");
        }

        // 提取出生日期字符串
        String birthDateStr = idCardNumber.substring(6, 14);

        // 将出生日期字符串解析为LocalDate
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate birthDate = LocalDate.parse(birthDateStr, formatter);

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 计算年龄
        return Period.between(birthDate, currentDate).getYears();
    }


}